﻿Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics

Namespace Global.Runa.UI.Controls

    Public Class Panel
        Inherits UIElementBase

        Public Property IsClip As Boolean

        Protected Friend Sub New(ByVal g As Game)
            MyBase.New(g)
        End Sub

        Public Sub New(ByVal Parent As UIElementBase)
            MyBase.New("", Parent)
            Me.Name = Me.GetType().Name & "_" & BytesToString(Guid.NewGuid().ToByteArray())
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase)
            MyBase.New(Name, Parent)
            Me.IsClip = False
        End Sub

        Public Overrides Sub Draw(ByVal SpriteBatch As SpriteBatch)
            If MyBase.Visible Then
                If (Not MyBase.Background Is Nothing) Then
                    If (Not MyBase.Effect Is Nothing) Then
                        SpriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend, Nothing, Nothing, Nothing, MyBase.Effect.Effect)
                    Else
                        SpriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend)
                    End If
                    If (Not MyBase.Background.Texture Is Nothing) Then
                        SpriteBatch.Draw(MyBase.Background.Texture, Me.DrawBounds, Nothing, Color.White * MyBase.Opacity, Me.AbsoluteRotation, MyBase.Orgin, MyBase.SpriteEffect, 0.5!)
                    End If
                    SpriteBatch.End()
                End If
                For Each item In Me.Children.OrderBy(Function(u) u.ZIndex)
                    If Me.IsClip Then
                        If (Me.AbsoluteBounds.Contains(item.AbsoluteBounds) Or Me.AbsoluteBounds.Intersects(item.AbsoluteBounds)) Then
                            item.Draw(SpriteBatch)
                        End If
                    Else
                        item.Draw(SpriteBatch)
                    End If
                Next
            End If
            MyBase.Draw(SpriteBatch)
        End Sub

        Public Overrides Sub Update(ByVal gameTime As GameTime)
            Dim ctr As UIElementBase
            For Each ctr In MyBase.Children
                ctr.Update(gameTime)
            Next
            MyBase.Update(gameTime)
        End Sub

    End Class

End Namespace